package com.mtao.impl;

import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import com.mtao.dto.DashscopeDto;
import com.mtao.FileUploadService;
import com.mtao.utils.FileOssUtil;
import com.mtao.utils.FileUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Value("${upload.strategy}")
    private String strategy;
    @Resource
    private FileOssUtil fileOssUtil;
    @Resource
    private FileUtil fileUtil;



    @Override
    public String upload(MultipartFile file) throws Exception {
        String pathUrl=null;
        if(strategy.equals("oss"))
            pathUrl= fileOssUtil.fileOss(file);
        else if(strategy.equals("local"))
            pathUrl= fileUtil.upload(file);
        return pathUrl;
    }

    /**
     * Ai 图像生成
     * @param dashscopeDto
     * @return
     */
    @Override
    public String createImg(DashscopeDto dashscopeDto) throws NoApiKeyException {
        try{
        String taskId = "89180";
        ImageSynthesis is = new ImageSynthesis();
//        # linux
//        export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
//
        //#windows
//        setx DASHSCOPE_API_KEY "YOUR_DASHSCOPE_API_KEY"
        Constants.apiKey="sk-335743b8203a4ab1af44884939fd76fb";
        ImageSynthesisParam param =
                ImageSynthesisParam.builder()
                        .model(ImageSynthesis.Models.WANX_V1)
                        .n(4)
                        .size("1024*1024")
                        .prompt("雄鹰自由自在的在蓝天白云下飞翔")
                        .build();
        ImageSynthesisResult result = is.call(param);
        if(result.getOutput().getCode()==null && result.getOutput().getMessage()==null){
//            return result.getOutput().getResults();
        }
        System.out.println();
        System.out.println(result);
        }catch(ApiException|NoApiKeyException e){
            System.out.println(e.getMessage());
        }
        return "dsad";
    }
}
